/**
 * 游戏开始界面
 */
class GameContainer extends egret.DisplayObjectContainer {
	// 炮台
	private _turretSprite: TurretSprite;
	// 炮台转动时间
	private _turretTurnTime: number;
	// 子弹是否是发射中
	private _bulletRuning: boolean = false;
	// 子弹类型数组   
	private _bulletTypes: Array<BulletType> = [];
	// 射出的子弹   
	public _bulletSpriteRadiates: Array<BulletSprite> = [];
	// 子弹发射速度
	public _bulletRadiateSpeed: number;

	public constructor() {
		super();
	}

	/**
	 * 游戏初始化
	 */
	public initGame(): void {
		/** 背景开始 */
		var shape: egret.Shape = new egret.Shape();
		shape.graphics.lineStyle(2, 150);
		shape.graphics.drawRect(0, 0, this.stage.stageWidth, this.stage.stageHeight);
		this.addChild(shape);
		/** 背景结束 */

		// 炮台转动时间
		this._turretTurnTime = 50;

		// 子弹发射速度
		this._bulletRadiateSpeed = 12;

		// 加载子弹类型
		this._bulletTypes.push(new BulletType(RES.getRes("hongqiu_json"), RES.getRes("ball_png"), 1));
		this._bulletTypes.push(new BulletType(RES.getRes("huangqiu_json"), RES.getRes("ball_png"), 2));
		this._bulletTypes.push(new BulletType(RES.getRes("lanqiu_json"), RES.getRes("ball_png"), 3));
		this._bulletTypes.push(new BulletType(RES.getRes("lvqiu_json"), RES.getRes("ball_png"), 4));
		this._bulletTypes.push(new BulletType(RES.getRes("ziqiu_json"), RES.getRes("ball_png"), 5));

		this.loadConfig();
		this.loadTurretSprite();
	}

	/**
	 * 加载配置文件
	 */
	private loadConfig(): void {
		var data = {
			turret_pos: { x: 200, y: 300 },
			routes: [
				{ x: 96, y: 989 },
				{ x: 100, y: 911 },
				{ x: 103, y: 836 },
				{ x: 103, y: 759 },
				{ x: 103, y: 670 },
				{ x: 93, y: 581 },
				{ x: 98, y: 510 },
				{ x: 98, y: 428 },
				{ x: 105, y: 335 },
				{ x: 117, y: 255 },
				{ x: 138, y: 192 },
				{ x: 199, y: 145 },
				{ x: 269, y: 135 },
				{ x: 375, y: 140 },
				{ x: 433, y: 189 },
				{ x: 487, y: 250 },
				{ x: 532, y: 314 },
				{ x: 550, y: 393 },
				{ x: 569, y: 482 },
				{ x: 574, y: 592 },
				{ x: 576, y: 707 },
				{ x: 576, y: 773 },
				{ x: 576, y: 874 },
				{ x: 574, y: 965 },
				{ x: 571, y: 1082 },
				{ x: 571, y: 1160 }
			],
			balls: [0, 1, 2, 3],
			start_pos: 2
		};
		config.Data.setData(data);
	}

    /**
     * 加载炮塔
     */
	private loadTurretSprite(): void {
		// 炮塔初始化
		this._turretSprite = new TurretSprite();
		this._turretSprite.x = this._turretSprite.width / 2 + config.Data.getData().turret_pos.x;
		this._turretSprite.y = this._turretSprite.height / 2 + config.Data.getData().turret_pos.y;
		this.addChild(this._turretSprite);

		// 填充子弹
		this._turretSprite.fillBullet(this._bulletTypes[0]);
		this._turretSprite.fillBullet(this._bulletTypes[1]);

		// 触摸事件注册（改变炮塔口的位置）
		this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onTouchBegin, this);
		// 触摸移动事件（改变炮塔口的位置）
		this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.onTouchMove, this);
		// 触摸放开（发射子弹）
		this.stage.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
		// 帧事件
		this.stage.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
	}

	/**
	 * 触摸开始
	 */
	private onTouchBegin(event: egret.TouchEvent) {
		var touchX = event.localX;
		var touchY = event.localY;
		var turretSprite = this._turretSprite;

		var angle = 180 / Math.PI * Math.atan2(touchX - turretSprite.x, turretSprite.y - touchY);

		turretSprite.rotation = angle;
		// 缓动设置炮口位置
		// egret.Tween.get(turretSprite).to({ rotation: angle }, this._turretTurnTime, egret.Ease.sineIn);
	}

	/**
	 * 触摸移动
	 */
	private onTouchMove(event: egret.TouchEvent) {
		var touchX = event.localX;
		var touchY = event.localY;
		var turretSprite = this._turretSprite;

		var angle = 180 / Math.PI * Math.atan2(touchX - turretSprite.x, turretSprite.y - touchY);

		turretSprite.rotation = angle;
	}

	/**
	 * 触摸结束
	 */
	private onTouchEnd(event: egret.TouchEvent) {
		if (!this._bulletRuning) {
			egret.setTimeout(function () {
				var random: number = Math.random() * (this._bulletTypes.length - 1);
				var turretSprite = this._turretSprite;
				turretSprite.radiateBullet();
				this._bulletRuning = true;
				turretSprite.fillBullet(this._bulletTypes[Math.floor(random)]);
			}, this, this._turretTurnTime);
		}

	}

	/**
	 * 帧调用
	 */
	private onEnterFrame(event: egret.TouchEvent) {
		// 子弹运动
		this.bulletMove();

		// 球运动
		this.ballMove();
	}

	/**
	 * 子弹运动
	 */
	private bulletMove() {
		var bulletSprite: BulletSprite;
		var bulletSpriteWidth_2: number, bulletSpriteHeight_2: number;
		var turretRadian: number, turretDistance: number, turretPoint: egret.Point;
		var bulletX: number, bulletY: number;
		for (var i: number = 0; i < this._bulletSpriteRadiates.length; i++) {
			bulletSprite = this._bulletSpriteRadiates[i];
			bulletSpriteWidth_2 = bulletSprite.width / 2;
			bulletSpriteHeight_2 = bulletSprite.height / 2;

			// 超出屏幕移除
			if (bulletSprite.x > this.stage.stageWidth + bulletSpriteWidth_2
				|| bulletSprite.x < -bulletSpriteWidth_2
				|| bulletSprite.y < - bulletSpriteHeight_2
				|| bulletSprite.y > this.stage.stageHeight + bulletSpriteHeight_2) {
				this._bulletSpriteRadiates.splice(i, 1);
				this.removeChild(bulletSprite);
				this._bulletRuning = false;
				return;
			}

			turretRadian = bulletSprite._turretRadian;
			turretDistance = bulletSprite._turretDistance = bulletSprite._turretDistance + this._bulletRadiateSpeed;
			turretPoint = bulletSprite._turretPoint;

			bulletX = turretPoint.x + turretDistance * Math.sin(turretRadian);
			bulletY = turretPoint.y - turretDistance * Math.cos(turretRadian);
			bulletSprite.x = bulletX;
			bulletSprite.y = bulletY;
			bulletSprite.changeFrameRate(this._bulletRadiateSpeed * 15);
		}
	}

	/**
	 * 球滚动
	 */
	private ballMove() {

	}
}